package com.irdstudio.efp.flow.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.efp.flow.service.facade.BizFlowTaskParamService;
import com.irdstudio.efp.flow.service.vo.BizFlowTaskParamVO;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.core.log.LogManager;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class BizFlowTaskParamController extends AbstractController  {
	
	@Autowired
	@Qualifier("bizFlowTaskParamService")
	private BizFlowTaskParamService bizFlowTaskParamService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/biz/flow/task/params", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<BizFlowTaskParamVO>> queryBizFlowTaskParamAll(
			@RequestBody BizFlowTaskParamVO vo) {		
		
		LogManager.info(vo.getTaskId(), bizFlowTaskParamService, "queryAll");
		
		//List<BizFlowTaskParamVO> outputVo = this.executeQueryList("queryAll", bizFlowTaskParamService, new Object[]{vo});
		
		List<BizFlowTaskParamVO> outputVo = null;
				
		// 根据用户角色获取用户权限方案
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		switch (dataRuleType) {
		case Owner:
			outputVo = bizFlowTaskParamService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = bizFlowTaskParamService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = bizFlowTaskParamService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = bizFlowTaskParamService.queryAllOwner(vo);
			break;
		}
		
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据任务查询参数
	 * @return
	 */
	@RequestMapping(value="/biz/flow/task/paramsByTask", method=RequestMethod.GET)
	public @ResponseBody ResponseData<List<BizFlowTaskParamVO>> queryBizFlowTaskParamByTaskId(@RequestParam("taskId") String taskId) {
		List<BizFlowTaskParamVO> outputVo = bizFlowTaskParamService.queryTaskParamsByTaskId(taskId);
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/biz/flow/task/param/{taskId}{bizParamCode}/{bizPluginId}/{bizParamIo}/", method=RequestMethod.GET)
	public @ResponseBody ResponseData<BizFlowTaskParamVO> queryByPk(@PathVariable("taskId") String taskId,@PathVariable("bizParamCode") String bizParamCode,@PathVariable("bizPluginId") String bizPluginId,@PathVariable("bizParamIo") String bizParamIo) {		
		BizFlowTaskParamVO inVo = new BizFlowTaskParamVO();
				inVo.setTaskId(taskId);
				inVo.setBizParamCode(bizParamCode);
				inVo.setBizPluginId(bizPluginId);
				inVo.setBizParamIo(bizParamIo);
		BizFlowTaskParamVO outputVo = bizFlowTaskParamService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param bizFlowTaskParam
	 * @return
	 */
	@RequestMapping(value="/biz/flow/task/param", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody BizFlowTaskParamVO inBizFlowTaskParamVo) {		
		int outputVo = bizFlowTaskParamService.deleteByPk(inBizFlowTaskParamVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inBizFlowTaskParamVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/task/param", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody BizFlowTaskParamVO inBizFlowTaskParamVo) {		
		int outputVo = bizFlowTaskParamService.updateByPk(inBizFlowTaskParamVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inBizFlowTaskParamVo
	 * @return
	 */
	@RequestMapping(value="/biz/flow/task/param", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertBizFlowTaskParam(@RequestBody BizFlowTaskParamVO inBizFlowTaskParamVo) {
		int outputVo = bizFlowTaskParamService.insertBizFlowTaskParam(inBizFlowTaskParamVo);
		return getResponseData(outputVo);
		
	}
}
